# bash declare completion                                  -*- shell-script -*-

_comp_cmd_declare()
{
    local cur prev words cword
    _init_completion -n := || return

    if [[ $cur == [-+]* ]]; then
        local opts
        opts=($(_parse_usage "$1"))
        # Most options also have a '+' form.
        # We'll exclude the ones that don't with compgen.
        opts+=(${opts[*]/-/+})
        COMPREPLY=($(compgen -W "${opts[*]}" -X '+[Ffgp]' -- "$cur"))
        return
    fi

    local i=1
    while [[ ${words[i]} == [-+]* ]]; do
        case ${words[i]} in
            -*[aA]*)
                COMPREPLY=($(compgen -A arrayvar -- "$cur"))
                return
                ;;
            -*[fF]*)
                COMPREPLY=($(compgen -A function -- "$cur"))
                return
                ;;
        esac
        ((i++))
    done
    if ((i > 1)); then
        # There was at least one option and it was not one that limited
        # operations to functions and array variables
        _comp_variable_assignments $cur && return
        COMPREPLY=($(compgen -A variable -- "$cur"))
    fi
} &&
    complete -F _comp_cmd_declare declare typeset

# ex: filetype=sh
